package com.zky.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zky.mapper.portalMapper;
import com.zky.pojo.HeadLine;
import com.zky.pojo.PortalVo;
import com.zky.pojo.portal;
import com.zky.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class portalServiceImpl implements portalService{


    @Autowired
    private portalMapper portalMapper;

    @Override
    public Result findNewPage(PortalVo portalVo) {
        System.out.println("portalVo.getPageNum():"+portalVo.getPageNum());
        PageHelper.startPage(portalVo.getPageNum(),portalVo.getPageSize());
        List<Map> newPage = portalMapper.findNewPage(portalVo);
        PageInfo<Map> pageInfo = new PageInfo<>(newPage);
        Map<String,Object> resultMap = new HashMap<>();
        Map List=new HashMap();
        List.put("pageData",pageInfo.getList());
        List.put("pageNum",pageInfo.getPageNum());
        List.put("pageSize",pageInfo.getPageSize());
        List.put("totalPage",pageInfo.getPages());
        List.put("totalSize",pageInfo.getTotal());
        resultMap.put("pageInfo",List);
        System.out.println(resultMap);
        return Result.ok(resultMap);
    }

    @Override
    public Result findAllTypes() {
        List<portal> portals = portalMapper.selectAllPortal();
        return Result.ok(portals);
    }
    @Override
    public Result showHeadlineDetail(Integer hid) {
        Map headLineDetail = portalMapper.showHeadlineDetail(hid);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("headline",headLineDetail);
        HeadLine headLine = new HeadLine();
        headLine.setPageViews((Integer) headLineDetail.get("pageViews")+1);
        headLine.setVersion((Integer) headLineDetail.get("version"));
        headLine.setHid((Integer) headLineDetail.get("hid"));
        int i = portalMapper.updateHeadline(headLine);
        return Result.ok(resultMap);
    }
    @Override
    public Result publish(HeadLine headLine)
    {
        headLine.setCreateTime(new Date());
        headLine.setUpdateTime(new Date());
        portalMapper.insert(headLine);
        return Result.ok(null);
    }

    @Override
    public Result findHeadLineByHid(Integer hid) {
        HeadLine headLineByHid = portalMapper.findHeadLineByHid(hid);
        Map<String,Object> result = new HashMap<>();
        result.put("headline",headLineByHid);
        return Result.ok(headLineByHid);
    }

    @Override
    public Result updateHeadlineMessage(HeadLine headLine) {
        int i = portalMapper.updateHeadlineMessage(headLine);
        return Result.ok(null);
    }

    @Override
    public Result removeByHid(Integer hid) {
        int delete = portalMapper.delete(hid);
        return Result.ok(null);

    }
}
